const mysql = require("mysql");
const {mysqlConfig} = require("../config.js");

const pool = mysql.createPool(mysqlConfig)

const mysqlService = (sql, params) => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
            if (err) {
                console.log(err);
                reject(err);
            } else {
                connection.query(sql, params, (err, result) => {
                    connection.release();  // 释放连接
                    if (err) {
                        console.log(err);
                        reject(err);
                    } else {
                        resolve(result);
                    }
                });
            }
        });
    });
};

module.exports = {
    mysqlService: mysqlService
}
